package com.csoa.community.db;

import java.util.ArrayList;
import java.util.List;

import android.database.Cursor;

import com.csoa.community.bean.PhotoBean;

public class PhotoDB extends DataBaseDB {

	/**
	 * 登陆成功保存数据到本地
	 * 
	 * @param lm
	 */
	public static void insert(PhotoBean photoBean) {
		DataBaseDB.getInst().getDB();
		String sql = "insert into photoTable(ownId,type,mediaType,path,title,photoNumber,author,remarks,time) values(?,?,?,?,?,?,?,?,?)";
		db.beginTransaction();
		try {
			db.execSQL(sql, new String[] {
					String.valueOf(photoBean.ownId),
					String.valueOf(photoBean.type),
					String.valueOf(photoBean.mediaType),
					photoBean.path,
					photoBean.title, 
					photoBean.photoNumber,
					photoBean.author, 
					photoBean.remarks,
					String.valueOf(System.currentTimeMillis()) });
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (db.isOpen()) {
				db.setTransactionSuccessful();
				db.endTransaction();
				db.close();
			}
		}
	}
	
	
	public static void insert(List<PhotoBean> photoBeans) {
		if(photoBeans == null || photoBeans.size() == 0) {
			return;
		}
		DataBaseDB.getInst().getDB();
		String sql = "insert into photoTable(ownId,type,mediaType,path,title,photoNumber,author,remarks,time) values(?,?,?,?,?,?,?,?,?)";
		db.beginTransaction();
		try {
			for(int i=0;i<photoBeans.size();i++) {
				PhotoBean photo = photoBeans.get(i);
				db.execSQL(sql, new String[] {
						String.valueOf(photo.ownId),
						String.valueOf(photo.type),
						String.valueOf(photo.mediaType),
						photo.path,
						photo.title, 
						photo.photoNumber,
						photo.author, 
						photo.remarks,
						String.valueOf(System.currentTimeMillis()) });
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (db.isOpen()) {
				db.setTransactionSuccessful();
				db.endTransaction();
				db.close();
			}
		}
	}

	/**
	 * 查询某个用户的登录信息
	 * @param lm
	 */
	public static List<PhotoBean> query(int type,int ownId,int mediaType) {
		DataBaseDB.getInst().getDB();
		String sql = "select id,ownId,type,mediaType,path,title,photoNumber,author,remarks,time from photoTable where type=? and ownId=? and mediaType=?";
		db.beginTransaction();
		Cursor cursor = null;
		List<PhotoBean> photoBeans = null;
		try {
			cursor = db.rawQuery(sql,new String[] {type + "",ownId + "",mediaType + ""});
			if (cursor.getCount() > 0) {
				photoBeans = new ArrayList<PhotoBean>();
				while (cursor.moveToNext()) {
					PhotoBean photoBean = new PhotoBean();
					photoBean.id = cursor.getInt(cursor.getColumnIndex("id"));
					photoBean.ownId = cursor.getInt(cursor.getColumnIndex("ownId"));
					photoBean.type = cursor.getInt(cursor.getColumnIndex("type"));
					photoBean.mediaType = cursor.getInt(cursor.getColumnIndex("mediaType"));
					photoBean.path = cursor.getString(cursor.getColumnIndex("path"));
					photoBean.title = cursor.getString(cursor.getColumnIndex("title"));
					photoBean.photoNumber = cursor.getString(cursor.getColumnIndex("photoNumber"));
					photoBean.author = cursor.getString(cursor.getColumnIndex("author"));
					photoBean.remarks = cursor.getString(cursor.getColumnIndex("remarks"));
					photoBean.time = cursor.getLong(cursor.getColumnIndex("nickName"));
					photoBeans.add(photoBean);
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null) {
				cursor.close();
				cursor = null;

			}
			if (db.isOpen()) {
				db.setTransactionSuccessful();
				db.endTransaction();
				db.close();
			}
		}
		return photoBeans;
	}




	/**
	 * 删除某一个用户的登录信息
	 * 
	 * @param PhotoBean
	 */
	public static void delete(PhotoBean photoBean) {
		DataBaseDB.getInst().getDB();
		String del = "delete from photoTable where id=?";
		db.beginTransaction();
		try {
			db.execSQL(del, new String[] { photoBean.id  + ""});
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (db.isOpen()) {
				db.setTransactionSuccessful();
				db.endTransaction();
				db.close();
			}
		}

	}

	/**
	 * 更新某个用户的登录信息
	 * 
	 * @param lm
	 */
//	public static boolean update(PhotoBean photoBean,String id) {
//		DataBaseDB.getInst().getDB();
//		String sql = "update photoTable set ownId=?,type=?,mediaType=?,title=?,photoNumber=?,author=?,remarks=? where id=?";
//		db.beginTransaction();
//		try {
//			db.execSQL(
//					sql,
//					new String[] {
//							photoBean.title,
//							photoBean.photoNumber, 
//							photoBean.author,
//							photoBean.remarks,
//							id});
//		} catch (Exception e) {
//			e.printStackTrace();
//			return false;
//		} finally {
//			if (db.isOpen()) {
//				db.setTransactionSuccessful();
//				db.endTransaction();
//				db.close();
//			}
//		}
//		return true;
//	}
	
	
	/**
	 * 更新某个用户的登录信息
	 * 
	 * @param lm
	 */
	public static boolean update(List<PhotoBean> photoBeans) {
		DataBaseDB.getInst().getDB();
		String sql = "update photoTable set ownId=?,type=?,mediaType=?,path=?,title=?,photoNumber=?,author=?,remarks=? where id=?";
		db.beginTransaction();
		try {
			
			for(int i=0;i<photoBeans.size();i++) {
				PhotoBean photoBean = photoBeans.get(i);
				db.execSQL(
						sql,
						new String[] {
								String.valueOf(photoBean.ownId),
								String.valueOf(photoBean.type),
								String.valueOf(photoBean.mediaType),
								photoBean.path,
								photoBean.title,
								photoBean.photoNumber, 
								photoBean.author,
								photoBean.remarks,
								String.valueOf(photoBean.id)
								});
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			if (db.isOpen()) {
				db.setTransactionSuccessful();
				db.endTransaction();
				db.close();
			}
		}
		return true;
	}
}
